<!-- 设备管理-设备运行-操作计划页面 -->
<template>
  <div>
    <div v-if="isMain" class="equip-wrap-common">
      <px-form ref="queryRef" class="custom-form-ui equip-form" :model="queryParams" inline label-width="120px">
        <px-row :gutter="16">
          <px-col :span="5">
            <px-form-item label="设备编号/名称">
              <px-input
                v-model="queryParams.value"
                placeholder="请输入设备编号/名称"
                maxlength="50"
                clearable
                @blur="queryParams.value = $event.target.value.trim()"
                @keyup.enter="queryTable"
              >
                <template #suffix>
                  <px-icon style="cursor: pointer" @click="queryTable">
                    <search />
                  </px-icon>
                </template>
              </px-input>
            </px-form-item>
          </px-col>
          <px-col :span="5">
            <EquipClassSelect :className="queryParams.className" @changeClassName="changeClassName" />
          </px-col>
          <px-col :span="5">
            <EquipSystemTree
              :className="queryParams.className"
              :systemCode="queryParams.systemCode"
              @changeAllSystemCodes="changeAllSystemCodes"
              @changeSystemCodes="changeSystemCodes"
              @clearSystem="clearSystem"
            />
          </px-col>
          <px-col :span="5">
            <px-form-item label="状态类型">
              <px-select v-model="queryParams.downType" placeholder="请选择状态类型" filterable clearable style="width: 100%">
                <px-option v-for="item in downTypeList" :key="item.id" :label="item.name" :value="item.value" />
              </px-select>
            </px-form-item>
          </px-col>
          <px-col :span="4">
            <px-button type="primary" @click="queryTable">查询</px-button>
            <px-button @click="resetTable">重置</px-button>
          </px-col>
        </px-row>
      </px-form>
      <div class="equip-table-btn">
        <px-button v-has="'ALL_EQUIP_RUNMANAGE_OPERATEPLAN_ADD'" type="primary" @click="addBtn">新增操作计划</px-button>
      </div>
      <px-table :data="tableData" class="equip-table" height="calc(100vh - 304px)" border>
        <px-table-column label="序号" width="80" align="center" fixed="left">
          <template #default="{ $index }">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + $index + 1 }}
          </template>
        </px-table-column>
        <px-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :label="item.label"
          :min-width="item.minWidth"
          :align="item.align"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :fixed="item.fixed"
        >
          <template #default="scope">
            {{ scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-" }}
          </template>
        </px-table-column>
        <px-table-column label="操作" width="240" fixed="right" align="center">
          <template #default="scope">
            <px-tooltip content="详情" placement="top">
              <px-button v-has="'ALL_EQUIP_RUNMANAGE_OPERATEPLAN_VIEW'" link icon="Tickets" type="primary" @click="handleView(scope.row)" />
            </px-tooltip>
            <px-tooltip content="编辑" placement="top">
              <px-button
                v-has="'ALL_EQUIP_RUNMANAGE_OPERATEPLAN_EDIT'"
                link
                icon="EditPen"
                type="primary"
                @click="handleEdit(scope.row)"
                :disabled="scope.row.planStatus === 2 || scope.row.planStatus === 3"
              />
            </px-tooltip>
            <px-tooltip content="复制" placement="top">
              <px-button v-has="'ALL_EQUIP_RUNMANAGE_OPERATEPLAN_COPY'" link icon="DocumentCopy" type="primary" @click="handleCopy(scope.row)" />
            </px-tooltip>
            <px-tooltip content="确认" placement="top" v-if="scope.row.planStatus === 1">
              <px-button
                v-has="'ALL_EQUIP_RUNMANAGE_OPERATEPLAN_SUBMIT'"
                link
                icon="CircleCheckFilled"
                type="primary"
                @click="handleSubmit(scope.row)"
              />
            </px-tooltip>
            <px-tooltip content="已确认" placement="top" v-if="scope.row.planStatus === 2 || scope.row.planStatus === 3">
              <px-button link icon="CircleCheckFilled" type="primary" disabled />
            </px-tooltip>
            <px-tooltip content="生成记录" placement="top" v-if="scope.row.planStatus === 2">
              <px-button
                v-has="'ALL_EQUIP_RUNMANAGE_OPERATEPLAN_RECORD'"
                link
                icon="Notebook"
                type="primary"
                @click="handleSubmitRecord(scope.row)"
              />
            </px-tooltip>
            <px-tooltip content="已生成记录" placement="top" v-if="scope.row.planStatus === 3">
              <px-button link icon="Notebook" type="primary" disabled />
            </px-tooltip>
            <px-tooltip content="删除" placement="top">
              <px-button v-has="'ALL_EQUIP_RUNMANAGE_OPERATEPLAN_DELETE'" link icon="Delete" type="danger" @click="handleDelete(scope.row)" />
            </px-tooltip>
          </template>
        </px-table-column>
      </px-table>
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        class="equip-table-pagination"
        :total="total"
        @pagination="queryTable"
      />
    </div>
    <!-- 新增/编辑页面 -->
    <AddEquipDetail
      :isMain="isMain"
      :isView="isView"
      :drawerType="drawerType"
      :rowData="rowData"
      @queryTable="queryTable"
      @backMainPage="backMainPage"
    />
    <!-- 查看页面 -->
    <ViewEquipDetail :isMain="isMain" :isView="isView" :drawerType="drawerType" :rowData="rowData" @backMainPage="backMainPage" />
    <SubmitDialog :submitDialogVisible="submitDialogVisible" :rowData="rowData" @closeDialog="closeDialog" />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "ALL_EQUIP_RUNMANAGE_OPERATEPLAN"
});
import { onMounted, reactive, toRefs } from "vue";
import { PxMessage, PxMessageBox } from "px-ui";
import EquipClassSelect from "@/views/all/equip/components/EquipClassSelect.vue";
import EquipSystemTree from "@/views/all/equip/components/EquipSystemTree.vue";
import AddEquipDetail from "./components/AddEquipDetail.vue";
import ViewEquipDetail from "./components/ViewEquipDetail.vue";
import SubmitDialog from "./components/SubmitDialog.vue";
import { queryDictData } from "@/api/common";
import { deleteRunRecord, queryOperatePlan } from "@/api/equip/runManage";
import { useRouter } from "vue-router";

const router = useRouter();
const state = reactive<any>({
  isMain: true, // 主页面true 详情页面false
  isView: true, // 查看true 新增/编辑false
  queryParams: {
    pageNum: 1,
    pageSize: 20
  },
  tableColumns: [
    { prop: "eqpId", label: "设备编号", minWidth: "160", showOverflowTooltip: true, fixed: "left" },
    { prop: "eqpName", label: "设备名称", minWidth: "160", showOverflowTooltip: true },
    { prop: "downCauseName", label: "状态原因", minWidth: "120", showOverflowTooltip: true },
    { prop: "downTypeName", label: "状态类型", minWidth: "120", showOverflowTooltip: true },
    { prop: "startTime", label: "计划开始时间", minWidth: "160", showOverflowTooltip: true },
    { prop: "factStartTime", label: "实际开始时间", minWidth: "160", showOverflowTooltip: true },
    { prop: "endTime", label: "实际结束时间", minWidth: "160", showOverflowTooltip: true },
    { prop: "runTimes", label: "状态时长", minWidth: "140", showOverflowTooltip: true },
    { prop: "classNameDesc", label: "科室", minWidth: "80", showOverflowTooltip: true },
    { prop: "systemName", label: "系统", minWidth: "130", showOverflowTooltip: true }
    // { prop: "createName", label: "创建人", minWidth: "120", showOverflowTooltip: true },
    // { prop: "createTime", label: "创建时间", minWidth: "180", showOverflowTooltip: true }
  ],
  tableData: [], // 列表数据
  total: 0, // 列表数量
  downTypeList: [], // 设备运行状态下拉框
  rowData: {},
  drawerType: "add", // 默认新增
  systemCodes: [],
  submitDialogVisible: false
});

const { isMain, isView, queryParams, tableColumns, tableData, total, downTypeList, rowData, drawerType, systemCodes, submitDialogVisible } =
  toRefs(state);

onMounted(() => {
  queryTable();
  querySelectList();
});

// 搜索查询条件下拉框
const querySelectList = async () => {
  downTypeList.value = await queryDictData({ typeCode: "EQP_DOWN_TYPE" });
};
// 查询列表
const queryTable = async () => {
  const params = JSON.parse(JSON.stringify(queryParams.value));
  params.systemCodes = systemCodes.value;
  const res = await queryOperatePlan(params);
  tableData.value = res?.data || [];
  total.value = res?.total;
};
// 重置筛选条件和列表
const resetTable = async () => {
  queryParams.value = {
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  systemCodes.value = [];
  queryTable();
};
// 从新增/编辑、详情返回主页面
const backMainPage = () => {
  isMain.value = true;
};
// 查看
const handleView = (row: any) => {
  drawerType.value = "view";
  rowData.value = row;
  isMain.value = false;
  isView.value = true;
};
// 复制
const handleCopy = async row => {
  drawerType.value = "copy";
  rowData.value = row;
  isMain.value = false;
  isView.value = false;
};
// 新增
const addBtn = () => {
  drawerType.value = "add";
  isMain.value = false;
  isView.value = false;
};
// 编辑
const handleEdit = (row: any) => {
  drawerType.value = "edit";
  rowData.value = row;
  isMain.value = false;
  isView.value = false;
};
// 删除
const handleDelete = async (row: any) => {
  PxMessageBox.confirm("要删除当前操作计划吗？", "提示", {
    confirmButtonText: "删除",
    confirmButtonClass: "px-button--danger",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true
  })
    .then(async () => {
      await deleteRunRecord({
        id: row.id
      });
      PxMessage.success("删除成功");
      queryTable();
    })
    .catch(() => {});
};
// 确认
const handleSubmit = (row: any) => {
  submitDialogVisible.value = true;
  rowData.value = row;
};
// 生成记录
const handleSubmitRecord = (row: any) => {
  router.push(`/all/equip/runManage/record?planId=${row?.id}`);
};
// 关闭确认弹窗
const closeDialog = () => {
  submitDialogVisible.value = false;
  queryTable();
};

// 外部封装好的组件
// 科室组件调用父组件
const changeClassName = val => {
  queryParams.value.className = val;
  queryParams.value.systemCode = "";
  systemCodes.value = [];
};
// 系统组件调用父组件
const changeSystemCodes = (val, val2) => {
  queryParams.value.systemCode = val;
  systemCodes.value = val2;
};
// 系统组件调用父组件
const changeAllSystemCodes = (val, val2, val3, val4) => {
  queryParams.value.className = val2;
  queryParams.value.systemCode = val3;
  systemCodes.value = val4;
};
// 系统组件值改变触发
const clearSystem = val => {
  queryParams.value.systemCode = "";
  systemCodes.value = [];
};
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/table-list.scss");
</style>
